package cn.ljy.uem.service.impl;

import cn.ljy.authority.config.security.UserUtil;
import cn.ljy.authority.model.SysUser;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.common.utils.requests.RequestUtil;
import cn.ljy.uem.dao.SchoolCourseMapper;
import cn.ljy.uem.dao.SchoolDepartmentMapper;
import cn.ljy.uem.dao.SchoolMajorMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.model.SchoolCourse;
import cn.ljy.uem.model.SchoolDepartment;
import cn.ljy.uem.model.SchoolMajor;
import cn.ljy.uem.model.SchoolTeacher;
import cn.ljy.uem.model.query.SchoolCourseQuery;
import cn.ljy.uem.service.SchoolCourseService;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


/**
 * 课程模块业务实现
 *
 * @author 小锋
 * @date 2022/07/26
 */
@Service
@Slf4j
public class ISchoolCourseService extends IBaseService<SchoolCourse, SchoolCourseQuery> implements SchoolCourseService {

    @Autowired
    private SchoolCourseMapper mapper;

    @Autowired
    private SchoolMajorMapper schoolMajorMapper;

    @Autowired
    private SchoolDepartmentMapper departmentMapper;

    @Autowired
    private SchoolTeacherMapper teacherMapper;


    /**
     * 获取图片的网址
     * @param cover 资源封面路径（相对路径，不含项目网址）
     * @return 如果文件名为空，则返回文件名本身（也为空）
     */
    private String getCoverWebUrl(String cover){
        String imgWebUrl=null;
        if(StringUtils.hasLength(cover)){
            imgWebUrl= RequestUtil.getBasePath() +cover;
        }else{
            imgWebUrl=cover;
        }
        return imgWebUrl;
    }



    @Override
    protected void beforeGet(Serializable id) {
    }

    @Override
    protected void afterGet(SchoolCourse schoolCourse) {
        //组装封面信息
        schoolCourse.setCover(getCoverWebUrl(schoolCourse.getCover()));
        if(schoolCourse.getMajorId()!=null){
            SchoolMajor schoolMajor= schoolMajorMapper.findById(schoolCourse.getMajorId());
            schoolCourse.setSchoolMajor(schoolMajor);
        }
        if(schoolCourse.getDepartmentId()!=null){
            SchoolDepartment schoolDepartment=departmentMapper.findById(schoolCourse.getDepartmentId());
            schoolCourse.setSchoolDepartment(schoolDepartment);
        }
        if(schoolCourse.getTeacherId()!=null){
            schoolCourse.setSchoolTeacher(teacherMapper.findById(schoolCourse.getTeacherId()));
        }
    }

    @Override
    protected void beforeUpdate(SchoolCourse schoolCourse) {
        if (mapper.getExpireByCourseId(schoolCourse.getId())==true){
            throw new MyWebException("操作失败：不能更改已回收课程");
        }
    }

    @Override
    protected void beforeSave(SchoolCourse schoolCourse) {
        //添加课程 默认不弃用值
        if (schoolCourse.getExpired() == null) {
            schoolCourse.setExpired(false);
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {
        //获取当前登录信息
        SysUser sysUser = UserUtil.getLoginUser();
        System.out.println(sysUser);
        if (sysUser != null) {//如果当前登录账户存在
            //获取当前登录账户对应的教师信息
            if (teacherMapper.getTeacherByUserId(sysUser.getId()) != null) {
                //查找教师信息下对应的课程
                throw new MyWebException("操作失败：当前账户是教师账户，无权操作！");
            }
        }
    }

    @Override
    protected void afterFindAll(List<SchoolCourse> list) {
        for (SchoolCourse schoolCourse:list){
            //组装封面信息
            schoolCourse.setCover(getCoverWebUrl(schoolCourse.getCover()));
            //组装专业信息
            if(schoolCourse.getMajorId()!=null){
                SchoolMajor schoolMajor= schoolMajorMapper.findById(schoolCourse.getMajorId());
                schoolCourse.setSchoolMajor(schoolMajor);
            }
            if(schoolCourse.getDepartmentId()!=null){
                SchoolDepartment schoolDepartment=departmentMapper.findById(schoolCourse.getDepartmentId());
                schoolCourse.setSchoolDepartment(schoolDepartment);
            }
            if(schoolCourse.getTeacherId()!=null){
                schoolCourse.setSchoolTeacher(teacherMapper.findById(schoolCourse.getTeacherId()));
            }
        }
        super.afterFindAll(list);
    }




}
